package com.itheima.reggie.mapper;

import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface DishMapper {

    /*
    * 查询指定类别的菜品数量(根据类别id)
    * */
    @Select("select count(*) from dish where category_id=#{categoryId} and is_deleted=0")
    Integer countByCid(Long categoryId);


    /*
    * 插入菜品数据
    * */
    @Insert("insert into dish values(null,#{name},#{categoryId},#{price},#{code},#{image}," +
            "#{description},#{status},#{sort},#{createTime},#{updateTime},#{createUser},#{updateUser},0)")
    //默认情况:数据库表中这条数据有主键,但是dishDto的属性id为null
    //目标:执行插入后讲数据库生成的id数据返回设置给dishDto对象中id属性
    //解决方案:@Options
    //useGeneratedKeys = true  获取生成的主键
    //keyProperty = "id",keyColumn = "id"   讲数据库表中生成的id值赋值给disDto对象属性id
    @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")
    Integer insert(DishDto dishDto);


    /*
    * 分页查询(连表查):多表连接查询
    * */
    List<DishDto> findAll(@Param("name") String name);


    /*
    * 根据菜品id查询一个菜品(一个菜品和多个口味)
    * */
    DishDto findById(Long id);


    /*
    * 根据id修改菜品
    * */
    Integer updateById(DishDto dishDto);



    /*
    * 根据类别id查询菜品列表的方法
    */
    @Select("select * from dish where category_id=#{categoryId} and is_deleted=0 order by sort asc")
    List<Dish> findByCategoryId(Long categoryId);



    /*
    * 根据类别id和状态查询菜品列表
    * */
    @Select("select *from dish where category_id=#{categoryId} and status=#{status} and is_deleted=0 order by sort asc ")
    List<Dish> findByCategoryAndStatus(@Param("categoryId") Long categoryId, @Param("status") Integer status);
}
